import logging

from django.views.decorators.cache import cache_page
from django.views.decorators.http import require_GET

from Jmusic import settings
from utils.response import ApiResponse, RET

from musicSpider.main import Music

music = Music()

logger = logging.getLogger('music')


@cache_page(settings.RECOMMEND_SONGS_LIST_TIMEOUT)
@require_GET
def get_recommend_songs_list(request):
    """
    推荐歌单
    :param request:
    :return:
    """
    source = request.GET.get('source')
    if source and source not in music.SPIDER:
        return ApiResponse(RET.PARAMERR)
    try:
        data = music.get_recommend_songs_list(source)
        return ApiResponse(data=data)
    except Exception as e:
        logger.error('spider(get_recommend_songs_list) error %s' % e)
    return ApiResponse(RET.SERVERERR)


@cache_page(settings.RECOMMEND_SONGS_TIMEOUT)
@require_GET
def get_recommend_songs(request):
    """
    推荐歌曲
    :param request:
    :return:
    """
    source = request.GET.get('source')
    if source and source not in music.SPIDER:
        return ApiResponse(RET.PARAMERR)
    try:
        data = music.get_recommend_songs(source)
        return ApiResponse(data=data)
    except Exception as e:
        logger.error('spider(get_recommend_songs) error %s' % e)
    return ApiResponse(RET.SERVERERR)


@require_GET
def get_songs_list_by_category(request):
    """
    分类歌单
    :param request:
    :return:
    """
    source = request.GET.get('source')
    categoryId = request.GET.get('categoryId')
    if (source and (source not in music.SPIDER)) or \
            (not source) or \
            (not categoryId):
        return ApiResponse(RET.PARAMERR)
    try:
        data = music.get_songs_list_by_category(int(categoryId), source)
        return ApiResponse(data=data)
    except Exception as e:
        logger.error('spider(get_songs_list_by_category) error %s' % e)
    return ApiResponse(RET.SERVERERR)


@require_GET
def get_songs_list_detail(request):
    """
    歌单详情
    :param request:
    :return:
    """
    source = request.GET.get('source')
    listId = request.GET.get('listId')
    if (source and (source not in music.SPIDER)) or \
            (not source) or \
            (not listId):
        return ApiResponse(RET.PARAMERR)
    try:
        data = music.get_songs_list_detail(listId, source)
        return ApiResponse(data=data)
    except Exception as e:
        logger.error('spider(get_songs_list_detail) error %s' % e)
    return ApiResponse(RET.SERVERERR)


@require_GET
def get_song_detail(request):
    """
    歌曲详情
    :param request:
    :return:
    """
    source = request.GET.get('source')
    songMid = request.GET.get('songMid')
    if (source and (source not in music.SPIDER)) or \
            (not source) or \
            (not songMid):
        return ApiResponse(RET.PARAMERR)
    try:
        data = music.get_song_detail(songMid, source)
        return ApiResponse(data=data)
    except Exception as e:
        logger.error('spider(get_song_detail) error %s' % e)
    return ApiResponse(RET.SERVERERR)


@require_GET
def get_song_play_url(request):
    """
    歌曲地址
    :param request:
    :return:
    """
    source = request.GET.get('source')
    songMid = request.GET.get('songMid')
    if (source and (source not in music.SPIDER)) or \
            (not source) or \
            (not songMid):
        return ApiResponse(RET.PARAMERR)
    try:
        data = music.get_song_play_url(songMid, source)
        return ApiResponse(data=data)
    except Exception as e:
        logger.error('spider(get_song_play_url) error %s' % e)
    return ApiResponse(RET.SERVERERR)


@require_GET
def get_song_lyric(request):
    """
    歌词
    :param request:
    :return:
    """
    source = request.GET.get('source')
    songId = request.GET.get('songId')
    if (source and (source not in music.SPIDER)) or \
            (not source) or \
            (not songId):
        return ApiResponse(RET.PARAMERR)
    try:
        data = music.get_song_lyric(songId, source)
        return ApiResponse(data=data)
    except Exception as e:
        logger.error('spider(get_song_lyric) error %s' % e)
    return ApiResponse(RET.SERVERERR)


@require_GET
def get_mv_play_url(request):
    """
    MV 地址
    :param request:
    :return:
    """
    source = request.GET.get('source')
    mvId = request.GET.get('mvId')
    if (source and (source not in music.SPIDER)) or \
            (not source) or \
            (not mvId):
        return ApiResponse(RET.PARAMERR)
    try:
        data = music.get_mv_play_url(mvId, source)
        return ApiResponse(data=data)
    except Exception as e:
        logger.error('spider(get_mv_play_url) error %s' % e)
    return ApiResponse(RET.SERVERERR)


@cache_page(settings.TOP_LIST_TIMEOUT)
@require_GET
def get_top_list(request):
    """
    排行榜
    :param request:
    :return:
    """
    source = request.GET.get('source')
    topId = request.GET.get('topId')
    if (source and (source not in music.SPIDER)) or \
            (not topId):
        return ApiResponse(RET.PARAMERR)
    try:
        data = music.get_top_list(int(topId), source)
        return ApiResponse(data=data)
    except Exception as e:
        logger.error('spider(get_top_list) error %s' % e)
    return ApiResponse(RET.SERVERERR)


@require_GET
def get_search_song(request):
    """
    搜索
    :param request:
    :return:
    """
    source = request.GET.get('source')
    kw = request.GET.get('kw')
    if (source and (source not in music.SPIDER)) or \
            (not kw):
        return ApiResponse(RET.PARAMERR)
    try:
        data = music.get_search_song(kw, source)
        return ApiResponse(data=data)
    except Exception as e:
        logger.error('spider(get_search_song) error %s' % e)
    return ApiResponse(RET.SERVERERR)
